package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import db.Dbclose;
import db.Dbconn;
import entity4wms.Deliver;
import entity4wms.Entry;
import tools.ResultSetPrinter;
import dao.DAOdeliver;
import dao.DAOentry;


public final class DAOinventory {
    static Connection conn = null;
    static PreparedStatement pstmt = null;
    static ResultSet rs = null;

    public static void initInventory(){
        String sqlentry = "select gid,sum(enumber) as 求和  from entry,goods group by entry.gid";
        String sqldeliver = "select gid,sum(dnum) as 求和  from deliver,goods group by deliver.gid";

    }

    public static void addInventory (Entry entryNote) throws ClassNotFoundException, SQLException  {
        String sql1 = "insert into inventory values(null,?,?,null,?,current_time())";
        conn = Dbconn.getConnection();
        pstmt = conn.prepareStatement(sql1);
        pstmt.setInt(1, entryNote.getGid());
        pstmt.setInt(2, entryNote.getId());
        pstmt.setInt(3, entryNote.getNumber());
        pstmt.executeUpdate();
    }
    public static void addInventory (Deliver note) throws ClassNotFoundException, SQLException  {
        String sql1 = "insert into inventory values(null,?,null,?,?,CURRENT_TIME())";
        conn = Dbconn.getConnection();
        pstmt = conn.prepareStatement(sql1);
        pstmt.setInt(1, note.getGid());
        pstmt.setInt(2, note.getId());
        pstmt.setInt(3, -note.getNumber());
        pstmt.execute();
    }
    public static void queryInventory() throws ClassNotFoundException, SQLException{
        String sql = "select * from inventory ";
        conn = Dbconn.getConnection();
        pstmt = conn.prepareStatement(sql);
        ResultSet rs = pstmt.executeQuery();
        ResultSetPrinter.printResultSet(rs);
        System.out.println("finishd ");

    }

    public static void removeInventory( Integer id ) throws ClassNotFoundException, SQLException{
        String sql = "delete from inventory where id = ?";
        conn = Dbconn.getConnection();
        pstmt = conn.prepareStatement(sql);
        pstmt.setInt(1,id );
        pstmt.execute();
        System.out.println("finished!!");
    }
}

